package tech.runchen.gz5055.oms.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import tech.runchen.gz5055.oms.common.utils.OrderUtils;
import tech.runchen.gz5055.oms.common.utils.PageInfo;
import tech.runchen.gz5055.oms.common.utils.Query;
import tech.runchen.gz5055.oms.common.utils.StringUtils;
import tech.runchen.gz5055.oms.modules.system.entity.ReturnOrderEntity;
import tech.runchen.gz5055.oms.modules.system.mapper.ReturnOrderMapper;
import tech.runchen.gz5055.oms.modules.system.service.IReturnOrderDetailService;
import tech.runchen.gz5055.oms.modules.system.service.IReturnOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 退货单信息表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-12-20
 */
@Service
public class ReturnOrderServiceImpl extends ServiceImpl<ReturnOrderMapper, ReturnOrderEntity> implements IReturnOrderService {

    @Autowired
    private IReturnOrderDetailService returnOrderDetailService;

    @Override
    public PageInfo queryPage(Map<String, Object> params) {

        String returnOrderNumber = (String) params.get("returnOrderNumber");
        String pickupAddress = (String) params.get("pickupAddress");

        Object status = params.get("status");

        Object userType = params.get("userType");
        Object userId = params.get("userId");

        IPage<ReturnOrderEntity> page = this.page(
                new Query<ReturnOrderEntity>().getPage(params),
                new QueryWrapper<ReturnOrderEntity>()
                        .in(Objects.nonNull(status), "status", status)
                        .and(Integer.valueOf(userType.toString()) == 0, wrapper ->
                                wrapper.eq("initiate_user_id", userId)
                        )
                        .like(StringUtils.isNotEmpty(returnOrderNumber), "return_order_number", returnOrderNumber)
                        .like(StringUtils.isNotEmpty(pickupAddress),"pickup_address",pickupAddress)
                        .orderByAsc("status")
                        .orderByDesc("create_time")
        );

        return new PageInfo(page);
    }

    @Override
    public ReturnOrderEntity queryInfoByOrderId(long orderId) {
        return this.getOne(new QueryWrapper<ReturnOrderEntity>().eq("order_id", orderId).last("limit 1"));
    }

    @Override
    public boolean saveReturnOrder(ReturnOrderEntity returnOrder) {
        returnOrder.setReturnOrderNumber(OrderUtils.generateOrderNumber(returnOrder.getOrderId()));
        this.save(returnOrder);
        if (StringUtils.isNotNull(returnOrder.getReturnOrderDetailList())) {
            returnOrderDetailService.saveReturnOrderDetailBatch(returnOrder.getId(), returnOrder.getReturnOrderDetailList());
        }
        return true;
    }

    @Override
    public boolean updateReturnOrder(ReturnOrderEntity returnOrder) {
        return this.updateById(returnOrder);
    }

    @Override
    public void deleteReturnOrder(Long returnOrderId) {
        this.removeById(returnOrderId);
        returnOrderDetailService.deleteByReturnOrderIdBatch(returnOrderId);
    }
}
